/**
 * 玩家状态
 */
var fsm = require("state-machine");

const PlayerStatesEnum = {
    PlayerDead: "dead",
    PlayerStand: "stand",
    PlayerJump: "jump",
    PlayerStartedToMove: "move",
    PlayerShoot: "PlayerShoot",
    PlayerXAttack: "PlayerXAttack",
}

const PlayerAnimaEnemu = {
    RUN: "x_run",
    STAND: "x_stand",
    JUMP: "x_jump",
    XSHOOT: "x_shoot",
}
var PlayerStates = cc.Class({
    extends: cc.Component,
    properties: {

    },

    ctor: function () {
        this.inicializeStates();
    },

    inicializeStates: function () {
        this._fsm = new fsm({
            init: PlayerStatesEnum.PlayerStand,
            transitions: [
                { name: "playerJump", from: [PlayerStatesEnum.PlayerShoot, PlayerStatesEnum.PlayerStartedToMove, PlayerStatesEnum.PlayerStand], to: PlayerStatesEnum.PlayerJump },
                { name: "playerStand", from: [PlayerStatesEnum.PlayerStartedToMove, PlayerStatesEnum.PlayerXAttack, PlayerStatesEnum.PlayerJump, PlayerStatesEnum.PlayerShoot], to: PlayerStatesEnum.PlayerStand },
                { name: 'PlayerStartedToMove', from: [PlayerStatesEnum.PlayerStand, PlayerStatesEnum.PlayerShoot], to: PlayerStatesEnum.PlayerStartedToMove },
                { name: 'playerShoot', from: [PlayerStatesEnum.PlayerShoot, PlayerStatesEnum.PlayerStartedToMove, PlayerStatesEnum.PlayerStand], to: PlayerStatesEnum.PlayerShoot },
                { name: 'playerXAttack', from: [PlayerStatesEnum.PlayerShoot, PlayerStatesEnum.PlayerJump, PlayerStatesEnum.PlayerStartedToMove, PlayerStatesEnum.PlayerStand], to: PlayerStatesEnum.PlayerXAttack },
            ],
            methods: {
                onJump: function () { GameEvent.dispatch(GameEvent.PLAYER_EVENT, {state:PlayerStatesEnum.PlayerJump}) },
                onPlayerStand: function () { GameEvent.dispatch(GameEvent.PLAYER_EVENT,{state: PlayerStatesEnum.PlayerStand}) },
                onPlayerStartedToMove: function () { GameEvent.dispatch(GameEvent.PLAYER_EVENT, {state:PlayerStatesEnum.PlayerStartedToMove}) },
                onPlayerShoot: function () { GameEvent.dispatch(GameEvent.PLAYER_EVENT,{state: PlayerStatesEnum.PlayerShoot}) },
                onPlayerXAttack: function () { GameEvent.dispatch(GameEvent.PLAYER_EVENT,{state: PlayerStatesEnum.PlayerXAttack}) },
                onEnterState: function (evt) {
                    let from = evt.from;
                    let to = evt.to;
                    cc.log("from.....",from)
                    cc.log("to......",to);
                    // if (from === PlayerStatesEnum.PayerJump && to === PlayerStatesEnum.PlayerXAttack) {
                    //     GameEvent.dispatch(GameEvent.PLAYER_EVENT,{ data: "x_jumpSwrod",state: PlayerStatesEnum.PlayerXAttack })
                    // }else if (from === PlayerStatesEnum.PlayerJump && to  === PlayerStatesEnum.PlayerXAttack ){
                    //     GameEvent.dispatch(GameEvent.PLAYER_EVENT,{ data: "x_sword",state: PlayerStatesEnum.PlayerXAttack })
                    // }else if (from ===  PlayerStatesEnum.PlayerXAttack && to === PlayerStatesEnum.PlayerJump ){
                    //     GameEvent.dispatch(GameEvent.PLAYER_EVENT,{ data: "x_swrodToJump",state: PlayerStatesEnum.PlayerJump })
                    // }else if (from ===  PlayerStatesEnum.PlayerStand && to === PlayerStatesEnum.PlayerJump ){
                    //     GameEvent.dispatch(GameEvent.PLAYER_EVENT,{ data: "x_jump",state: PlayerStatesEnum.PlayerJump })
                    // }else if (from ===  PlayerStatesEnum.PlayerJump && to === PlayerStatesEnum.PlayerStand ){
                    //     GameEvent.dispatch(GameEvent.PLAYER_EVENT,{ data: "x_stand",state: PlayerStatesEnum.PlayerStand })
                    // }
                }
            }
        });
    },

    is: function (state) {
        return this._fsm.is(state);
    },

    getCurState: function () {
        return this._fsm.state;
    },

    setPlayerState(state) {
        if (this.getCurState() === state) {
            return;
        }
        if (state === PlayerStatesEnum.PlayerStand) {
            this._fsm.playerStand();
        } else if (state === PlayerStatesEnum.PlayerJump) {
            this._fsm.playerJump();
        } else if (state === PlayerStatesEnum.PlayerStartedToMove) {
            if (this.getCurState() === state) {
            //     GameEvent.dispatch(GameEvent.PLAYER_EVENT, PlayerStatesEnum.PlayerStartedToMove)
                return;
            }
            this._fsm.playerstartedtomove();
        } else if (state === PlayerStatesEnum.PlayerShoot) {
            // if (this.getCurState() === state) {
            //     GameEvent.dispatch(GameEvent.PLAYER_EVENT, PlayerStatesEnum.PlayerShoot)
            //     return;
            // }
            this._fsm.playerShoot();
        } else if (state === PlayerStatesEnum.PlayerXAttack) {
            
            this._fsm.playerXAttack();
        }
    },

    getPlayerStateValue: function (customEvent) {
        // return this._stateMap.get(customEvent);
    }

})

window.PlayerStates = new PlayerStates();

window.PlayerStates.PlayerStatesEnum = PlayerStatesEnum
window.PlayerStates.PlayerAnimaEnemu = PlayerAnimaEnemu